<template>
  <div>
    <el-dialog
      width="1024px"
      :close-on-click-modal="false"
      :show-close="true"
      v-bind="$attrs"
      v-on="$listeners"
      @open="onOpen"
      @close="onClose"
    >
      <div slot="title">
        {{ formData.id != null ? '修改回访' : '新增回访' }}
      </div>
      <div class="sketch_content">
        <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px"
                 label-position="top"
        >
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="回访编号" prop="visitSn">
                <el-input v-model="formData.visitSn" placeholder="支持手动输入，为空则根据规则自动生成"
                          :style="{width: '100%'}"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="回访时间" prop="visitTime">
                <el-date-picker v-model="formData.visitTime" format="yyyy-MM-dd HH:mm:ss"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                type="datetime" :style="{width: '100%'}" placeholder="选择回访时间"
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="回访人" prop="ownerUserId">
                <el-input v-model="formData.ownerUserName" placeholder="请输入回访人" :style="{width: '100%'}">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="回访形式" prop="returnVisitTypeId">
                <el-select v-model="formData.returnVisitTypeId" placeholder="请选择" style="width: 100%">
                  <el-option
                    v-for="dict in dict.type.crm_return_visit_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="客户名称" prop="customerId">
                <el-input @click.native="customerIdClick" placeholder="请选择客户" v-model="formData.customerName"
                          style="width: 100%"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系人" prop="contactsName">
                <el-input v-model="formData.contactsName" placeholder="请选择一个联系人" style="width: 100%"
                          :disabled="formData.customerId == null" @click.native="contactsIdClick"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="合同编号" prop="contractSn">
                <el-input v-model="formData.contractSn" placeholder="请选择合同" :disabled="formData.customerId == null"
                          prefix-icon="el-icon-mobile" :style="{width: '100%'}" @click.native="contractIdClick"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="客户满意度" prop="satisfactionId">
                <el-select v-model="formData.satisfactionId" placeholder="请选择" style="width: 100%">
                  <el-option
                    v-for="dict in dict.type.crm_satisfaction"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="客户反馈" prop="feedback">
                <el-input v-model="formData.feedback" type="textarea" placeholder="请输入客户反馈"
                          :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="submitForm" :loading="submitLoading">保 存</el-button>
        <el-button size="small" @click="onClose">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 客户选择 -->
    <customer-select-popover ref="customerSelectPopover" @callback="customerDataCallback"/>
    <!-- 联系人选择 -->
    <contacts-select-popover ref="contactsSelectPopover" @callback="contactsDataCallback"/>
    <!-- 合同选择 -->
    <contract-select-popover ref="contractSelectPopover" @callback="contractDataCallback"/>
  </div>
</template>
<script>
import {
  updateByPrimaryKeySelective,
  saveHasKey,
  selectByPrimaryKey
} from '@/api/crm/Visit'

import CustomerSelectPopover from '@/components/Crm/Customer/CustomerSelectPopover'
import ContactsSelectPopover from '@/components/Crm/Contacts/ContactsSelectPopover'
import ContractSelectPopover from '@/components/Crm/Contract/ContractSelectPopover'

export default {
  name: 'VisitDialog',
  inheritAttrs: false,
  components: {
    CustomerSelectPopover,
    ContactsSelectPopover,
    ContractSelectPopover
  },
  dicts: ['crm_return_visit_type', 'crm_satisfaction'],
  props: {
    pk: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      submitLoading: false,
      formData: {
        visitSn: undefined,
        visitTime: null,
        ownerUserId: undefined,
        returnVisitTypeId: undefined,
        customerId: undefined,
        customerName: '',
        contactsId: undefined,
        contactsName: '',
        contractSn: undefined,
        satisfactionId: undefined,
        feedback: undefined
      },
      rules: {
        visitTime: [{ required: true, message: '选择回访时间', trigger: 'change' }],
        // ownerUserId: [{ required: true, message: '请输入回访人', trigger: 'blur' }],
        returnVisitTypeId: [{ required: true, message: '请选择', trigger: ['change'] }],
        customerId: [{ required: true, message: '选择客户', trigger: ['blur', 'change'] }],
        contractSn: [{ required: true, message: '选择合同', trigger: ['blur', 'change'] }]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
  },
  mounted() {
  },
  methods: {
    onOpen() {
      // 读取本地用户数据
      if (this.pk != null) {
        selectByPrimaryKey(this.pk).then(res => {
          this.formData = res.data
        })
      }
    },
    onClose() {
      this.$refs['elForm'].resetFields()
      this.$emit('update:visible', false)
    },
    submitForm() {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          if (this.formData.id != undefined) {
            // 修改
            updateByPrimaryKeySelective(this.formData).then(res => {
              this.$modal.msgSuccess('修改成功')
              this.$emit('ok')
              this.onClose()
            })
          } else {
            // 新增
            saveHasKey(this.formData).then(res => {
              this.$modal.msgSuccess('添加成功')
              this.$emit('ok')
              this.onClose()
            })
          }
        }
      })
    },
    // 客户input点击事件
    customerIdClick() {
      this.$nextTick(() => {
        this.$refs.customerSelectPopover.open()
      })
    },
    // 客户弹出框回调
    customerDataCallback(data) {
      // 如果旧id不等于新id，则证明客户变更了，需要重置商机和客户签约人
      if (this.formData.customerId != data.id) {
        this.formData.contactsId = null
        this.formData.contactsName = ''
      }
      this.formData.customerId = data.id
      this.formData.customerName = data.customerName
    },
    // 联系人input点击事件
    contactsIdClick() {
      if (this.formData.customerId != null) {
        this.$nextTick(() => {
          this.$refs.contactsSelectPopover.open(this.formData.customerId, 2)
        })
      }
    },
    // 联系人弹出框回调
    contactsDataCallback(data) {
      console.log(data)
      this.formData.contactsId = data.id
      this.formData.contactsName = data.name
    },
    // 合同input点击事件
    contractIdClick() {
      if (this.formData.customerId != null) {
        this.$nextTick(() => {
          this.$refs.contractSelectPopover.open('invoice', this.formData.customerId)
        })
      }
    },
    // 合同弹出框回调
    contractDataCallback(data) {
      this.formData.contractId = data.id
      this.formData.contractSn = data.contractSn
      this.formData.contractName = data.contractName
    }
  }
}

</script>
<style>
</style>
